package com.seafwg.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seafwg.commonutils.JwtUtils;
import com.seafwg.educenter.entity.UcenterMember;
import com.seafwg.educenter.entity.vo.RegisterVo;
import com.seafwg.educenter.mapper.UcenterMemberMapper;
import com.seafwg.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seafwg.educenter.utils.MD5;
import com.seafwg.servicebase.exceptionhandler.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author seafwg
 * @since 2021-04-07
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate; //注入redis

    /**
     * 用户登录
     * @param member
     * @return
     */
    @Override
    public String login(UcenterMember member) {
        //获取用户登录信息：
        String mobile = member.getMobile();
        String password = member.getPassword();
        // 手机号和密码非空时抛出异常
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new MyException(20001, "登录失败");
        }
        //判断手机号是否正确,构造查询条件，调用baseMap
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper); //根据电话号码查出来的用户信息
        if(mobileMember == null) {//会员为空，没有电话号码
            throw new MyException(20001, "登录失败");
        }
        //判断密码,由于数据库中的密码是md5加密过的
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new MyException(20001, "登录失败");
        }
        //判断用户是否禁用
        if(mobileMember.getIsDisabled()) {
            throw new MyException(20001, "登录失败");
        }

        // 登录成功：生成token字符串，使用jwt工具类
        // 根据id和昵称生成token
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());

        return jwtToken;
    }

    /**
     * 用户注册
     * @param registerVo
     */
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的信息：
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        //判断非空
        if(StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname) | StringUtils.isEmpty(password)) {
            throw new MyException(20001, "注册失败");
        }
        //从redis中获取验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)) {
            throw new MyException(20001, "注册失败");
        }
        //判断手机号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0) {
            throw new MyException(20001, "注册失败");
        }
        //把数据添加到数据库中
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setPassword(password);
        member.setNickname(nickname);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        baseMapper.insert(member);
    }
}
